#ifndef CGRIDLAYOUTT_HPP
#define CGRIDLAYOUTT_HPP

#include <time.h>
#include <shlobj.h>
#include <shtypes.h>
#include <windows.h>
#include <strsafe.h>
#include <commctrl.h>
#include <vector>
#include <memory>

class CGridLayout
{
public:
    CGridLayout() {}
    ~CGridLayout() {}
    CGridLayout(LPCTSTR lpszClassName, HINSTANCE hInstance)
    {
        m_hWnd = NULL;
        m_hParent = NULL;
        m_wcex = {0};
        m_rcMenuBar = {0};
        m_rcToolBar = {0};
        Register(lpszClassName, hInstance);
    }
    CGridLayout(const CGridLayout &) = delete;
    CGridLayout &operator=(const CGridLayout &) = delete;
    HWND GetHWND() const
    {
        return m_hWnd;
    }
    void SetToolBarRect(RECT rcToolBar)
    {
        m_rcToolBar = rcToolBar;
    }
    void SetMenuBarRect(RECT rcMenuBar)
    {
        m_rcMenuBar = rcMenuBar;
    }
    LRESULT Register(LPCTSTR lpszClassName, HINSTANCE hInstance)
    {
        m_wcex = {0};
        m_wcex.cbSize = sizeof(WNDCLASSEX);
        m_wcex.cbClsExtra = 0;                                      // 类的额外内存
        m_wcex.cbWndExtra = 0;                                      // 窗口额外内存
        m_wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 背景
        m_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);               // 光标 第一个为NULL代表使用系统提供的光标
        m_wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);             // 图标 第一个为NULL代表使用系统提供的图标
        m_wcex.hInstance = hInstance;                               // 应用程序实例句柄
        m_wcex.lpfnWndProc = CGridLayout::WndProc;                  // 回调函数
        m_wcex.lpszClassName = lpszClassName;                       // 窗口类名称
        m_wcex.lpszMenuName = NULL;                                 // 菜单名称
        m_wcex.style = CS_HREDRAW | CS_VREDRAW;                     // 显示风格
        m_hInstance = hInstance;
        if (!RegisterClassEx(&m_wcex))
        {
            MessageBox(NULL, TEXT("Failed to register child window class"), TEXT("Error"), MB_OK | MB_ICONERROR);
            return -1;
        }
        return 0;
    }
    LRESULT Create(HWND hParent, LPRECT lpRect, LPCTSTR szWindowName)
    {
        // Create the child window
        RECT rect;
        if (!lpRect)
        {
            GetClientRect(hParent, &rect);
            lpRect = &rect;
        }
        m_nMenuBarHeight = m_rcMenuBar.bottom - m_rcMenuBar.top;
        m_nToolBarHeight = m_rcToolBar.bottom - m_rcToolBar.top;
        lpRect->top += m_nToolBarHeight;
        m_hWnd = CreateWindow(m_wcex.lpszClassName, szWindowName, WS_CHILD | WS_VISIBLE,
                              lpRect->left, lpRect->top, lpRect->right - lpRect->left, lpRect->bottom - lpRect->top,
                              hParent, NULL, m_hInstance, this);
        m_hParent = hParent;
        SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);
        return 0;
    }
    void FormatWindowClassName(_Out_writes_z_(dwBuffSize) LPTSTR szBuffer, _In_ DWORD dwBuffSize, _In_ void *unique)
    {
        StringCchPrintf(szBuffer, dwBuffSize, TEXT("grid:%p"), unique);
    }
    LRESULT SetGridSpacing(UINT uHorSpace, UINT uVerSpace)
    {
        // Horizontal Vertical
        m_uHorSpace = uHorSpace;
        m_uVerSpace = uVerSpace;
        m_hWndArray.resize(m_uHorSpace, std::vector<HWND>(m_uVerSpace));
        for (UINT uRow = 0; uRow < m_uHorSpace; uRow++)
        {
            for (UINT uCol = 0; uCol < m_uVerSpace; uCol++)
            {
                m_hWndArray[uRow][uCol] = NULL;
            }
        }
        for (UINT uRow = 0; uRow < m_uHorSpace; uRow++)
        {
            for (UINT uCol = 0; uCol < m_uVerSpace; uCol++)
            {
                WNDCLASSEX wcex = {0};
                TCHAR szAutoName[32];
                StringCchPrintf(szAutoName, 32, TEXT("r:%X,c:%X"), uRow, uCol);
                wcex.cbSize = sizeof(WNDCLASSEX);
                wcex.cbClsExtra = 0;                                                                       // 类的额外内存
                wcex.cbWndExtra = 0;                                                                       // 窗口额外内存
                wcex.hbrBackground = GetSysColorBrush(/*COLOR_3DFACE*/ COLOR_3DSHADOW /*COLOR_3DSHADOW*/); // 背景
                wcex.hCursor = LoadCursor(NULL, IDC_ARROW);                                                // 光标 第一个为NULL代表使用系统提供的光标
                wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);                                              // 图标 第一个为NULL代表使用系统提供的图标
                wcex.hInstance = m_hInstance;                                                              // 应用程序实例句柄
                wcex.lpfnWndProc = CGridLayout::GridWndProc;                                               // 回调函数
                wcex.lpszClassName = szAutoName;                                                           // 窗口类名称
                wcex.lpszMenuName = NULL;                                                                  // 菜单名称
                wcex.style = CS_HREDRAW | CS_VREDRAW;                                                      // 显示风格
                if (!RegisterClassEx(&wcex))
                {
                    MessageBox(NULL, TEXT("Failed to register child window class"), TEXT("Error"), MB_OK | MB_ICONERROR);
                    break;
                }
                RECT rcParent;
                GetClientRect(m_hWnd, &rcParent);
                UINT uLeft = ((rcParent.right - rcParent.left) / m_uVerSpace) * uCol;
                UINT uTop = ((rcParent.bottom - rcParent.top) / m_uHorSpace) * uRow;
                UINT uRight = ((rcParent.right - rcParent.left) / m_uVerSpace);
                UINT uBottom = ((rcParent.bottom - rcParent.top) / m_uHorSpace);
                // RECT wr = {uLeft, uTop, uRight, uBottom};
                // AdjustWindowRect(&wr, WS_CHILD | WS_VISIBLE, FALSE);
                m_hWndArray[uRow][uCol] = CreateWindowEx(WS_EX_CLIENTEDGE, wcex.lpszClassName, TEXT(""), WS_CHILD | WS_VISIBLE,
                                                         uLeft, uTop, uRight, uBottom,
                                                         //  wr.left, wr.top, wr.right, wr.bottom,
                                                         m_hWnd, NULL, m_hInstance, this);
            }
        }
        return 0;
    }
    LRESULT setContentsMargins()
    {
        return 0;
    }
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        CGridLayout *pThis = (CGridLayout *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
        if (pThis)
        {
            return pThis->WindowProc(hWnd, uMsg, wParam, lParam);
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            HPEN hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0));
            HPEN holdPen = (HPEN)SelectObject(hdc, hPen);
            HBRUSH hBrush = CreateSolidBrush(RGB(200, 255, 200));
            HBRUSH holdBrush = (HBRUSH)SelectObject(hdc, hBrush);

            RECT rect;
            GetClientRect(hWnd, &rect);
            Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);

            SelectObject(hdc, holdPen);
            SelectObject(hdc, holdBrush);
            DeleteObject(hPen);
            DeleteObject(hBrush);
            ReleaseDC(hWnd, hdc);
            EndPaint(hWnd, &ps);
            break;
        }
        case WM_COMMAND:
        {
            TCHAR szTitle[MAX_PATH] = {0};
            StringCchPrintf(szTitle, 256, TEXT("我收到了: %d"), LOWORD(wParam));
            // MessageBox(0, szTitle, TEXT("我收到了"), MB_OK);
            break;
        }
        case WM_SIZE:
        {
            RECT rcParent;
            GetClientRect(m_hParent, &rcParent);
            rcParent.top += m_rcToolBar.bottom - m_rcToolBar.top;
            rcParent.bottom -= m_rcToolBar.bottom - m_rcToolBar.top;
            MoveWindow(m_hWnd, rcParent.left, rcParent.top, rcParent.right, rcParent.bottom, TRUE);

            RECT rcWnd;
            GetClientRect(m_hWnd, &rcWnd);
            for (UINT uRow = 0; uRow < m_uHorSpace; uRow++)
            {
                for (UINT uCol = 0; uCol < m_uVerSpace; uCol++)
                {
                    if (m_hWndArray[uRow][uCol])
                    {
                        UINT uLeft = ((rcWnd.right - rcWnd.left) / m_uVerSpace) * uCol;
                        UINT uTop = ((rcWnd.bottom - rcWnd.top) / m_uHorSpace) * uRow;
                        UINT uRight = ((rcWnd.right - rcWnd.left) / m_uVerSpace);
                        UINT uBottom = ((rcWnd.bottom - rcWnd.top) / m_uHorSpace);
                        MoveWindow(m_hWndArray[uRow][uCol], uLeft, uTop, uRight, uBottom, TRUE);
                    }
                }
            }
            break;
        }
        case WM_MOUSEMOVE:
        {
            int xPos = LOWORD(lParam);
            int yPos = HIWORD(lParam);
            TCHAR buffer[256];
            StringCchPrintf(buffer, 256, TEXT("xPos: %d, yPos: %d"), xPos, yPos);
            // SetWindowText(m_hParent, buffer);
            break;
        }
        case WM_CLOSE:
        {
            DestroyWindow(hWnd);
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        default:
            break;
        }
        return DefWindowProc(m_hWnd, uMsg, wParam, lParam);
    }
    static LRESULT CALLBACK GridWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps;
        HDC hdc;
        RECT rect;
        static HBRUSH hleft_Wnd_brush = NULL;

        switch (message)
        {
        case WM_CREATE:
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            GetClientRect(hWnd, &rect);
            hleft_Wnd_brush = GetSysColorBrush(COLOR_3DSHADOW);
            FillRect(hdc, &rect, hleft_Wnd_brush);
            DeleteObject(hleft_Wnd_brush);
            EndPaint(hWnd, &ps);
            break;
        case WM_CLOSE:
        {
            DestroyWindow(hWnd);
        }
        break;
        case WM_DESTROY:
        {
            PostQuitMessage(0);
        }
        break;
        default:
            break;
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
    }

public:
    UINT m_nMenuBarHeight;
    UINT m_nToolBarHeight;
    HWND m_hWnd;
    HWND m_hParent;
    WNDCLASSEX m_wcex;
    RECT m_rcMenuBar;
    RECT m_rcToolBar;
    HINSTANCE m_hInstance;

    UINT m_uHorSpace = 0;
    UINT m_uVerSpace = 0;
    std::vector<std::vector<HWND>> m_hWndArray;
};

#endif